/**
 * 
 */
package com.jadobo.xswing.swing.style.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.jadobo.xswing.attrib.HtmlElementTag;
import com.jadobo.xswing.model.Font;
import com.jadobo.xswing.model.IElement;
import com.jadobo.xswing.model.Page;
import com.jadobo.xswing.model.Style;
import com.jadobo.xswing.swing.style.IStyleProcessor;
import com.jadobo.xswing.util.Elem;

/**
 * @author owen
 *
 */
public class DefaultStyleProcessor implements IStyleProcessor {

	private static final Logger LOG = Logger.getLogger(DefaultStyleProcessor.class);
			
	private Map<HtmlElementTag, Font> fontMap;

	/* (non-Javadoc)
	 * @see com.jadobo.xswing.swing.style.IStyleProcessor#processPage(com.jadobo.xswing.model.Page)
	 */
	@Override
	public void processPage(Page page) {
		
		// pre-condition check
		if(page == null || page.getBody() == null) {
			LOG.error("processPage(page): page parameter or page body is null.");
			return;
		}
		
		// get all elements in the tree
		List<IElement> list = Elem.getAllElements(page.getBody().getElements());
		
		// apply default font
		for(IElement element : list) {
			Style style = new Style();
			
			Font defaultFont = fontMap.get(element.getHtmlElementTag());
			
			if(defaultFont == null) {
				LOG.warn("No default font for element tag: " + element.getHtmlElementTag());
			}
			
			style.setFont(defaultFont);
			
			element.setStyle(style);
		}
		
		

	}

	/**
	 * @return the fontMap
	 */
	public Map<HtmlElementTag, Font> getFontMap() {
		return fontMap;
	}

	/**
	 * @param fontMap the fontMap to set
	 */
	public void setFontMap(Map<HtmlElementTag, Font> fontMap) {
		this.fontMap = fontMap;
	}
}
